package com.example.rydwlib

import android.app.Application
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.example.baselib.BaseViewModel
import com.example.baselib.constants.MyConstants
import com.example.baselib.databean.Unitinfo
import com.example.baselib.databean.UserData
import com.example.rydwlib.bean.*
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import kotlin.collections.ArrayList

/**
 * Author: sxygsj
 * Date: 2020/4/30 17:10
 * Description: 人员定位viewmodel
 * History:
 * <author> <time> <version> <desc>
 * 作者姓名 修改时间 版本号 描述
 */
class RydwViewModel(application: Application):BaseViewModel(application) {

    private val repository = RydwRepository()
    private val ryMainData:MutableLiveData<RyMainData> by lazy { MutableLiveData<RyMainData>().also { loadRyMainData() } }
    private val ryAlarmData:MutableLiveData<RyAlarmData> by lazy { MutableLiveData<RyAlarmData>().also { loadRyAlarmData() } }
    private lateinit var ryPersonData:MutableLiveData<List<RyPersonData>>
    private val ryReportData:MutableLiveData<RyReportData>by lazy { MutableLiveData<RyReportData>().also { loadRyReportData() } }
    private val userData: UserData = MyConstants.userData
    private  var unitinfo: Map<String,String> = HashMap<String,String>()
    fun setUnitInfo(unitinfo: Map<String,String>){
        this.unitinfo=unitinfo
    }
    fun getUnitInfo():  Map<String,String> {
        return unitinfo
    }
    fun getUserData(): UserData {
        return userData
    }
    fun  loadRyReportData()=launchUI{
        ryReportData.value= RyReportData(loadRyReportChart(),loadRyRegion())
    }

    private suspend fun loadRyReportChart():List<RyRegionCount>{
        val result=repository.loadRyReportChart(userData.token, unitinfo.getValue("unitcode"))
        val type = object : TypeToken<List<RyRegionCount>>() {}.type
        return Gson().fromJson<List<RyRegionCount>>(result.body()?.data, type)
    }
    fun loadRyMainData() = launchUI{
        val coutData=repository.loadRyMainData(userData.token, unitinfo.getValue("unitcode"))
        val type = object : TypeToken<RyCountData>() {}.type
        ryMainData.value= RyMainData(Gson().fromJson<RyCountData>(coutData.body()?.data, type), loadRyRegion())
    }
    fun loadRyAlarmData() = launchUI {
       val result= repository.loadRyAlarmData(userData.token,unitinfo.getValue("unitcode"))
        val type = object : TypeToken<RyAlarmData>() {}.type
        ryAlarmData.value= Gson().fromJson<RyAlarmData>(result.body()?.data, type)
    }

    fun loadRyPersonData(type:String)=launchUI{
        val result=repository.loadRyPersonData(type,userData.token, unitinfo.getValue("unitcode"))
        val resulttype = object : TypeToken<List<RyPersonData>>() {}.type
        ryPersonData.value= Gson().fromJson<List<RyPersonData>>(result.body()?.data, resulttype)
    }

    private suspend fun loadRyRegion():ArrayList<ArrayList<RyRegionData>>{
        val regionData=repository.loadRyRegionData(userData.token, unitinfo.getValue("unitcode"))
        val type = object : TypeToken<List<RyRegionData>>() {}.type
        val regionlist= Gson().fromJson<List<RyRegionData>>(regionData.body()?.data, type)
        val iterator = regionlist.iterator()

        val map:HashMap<String,ArrayList<RyRegionData>> = HashMap()

        while (iterator.hasNext()) {
            val bean = iterator.next()
            if (map.contains(bean.regiontype)){
                map[bean.regiontype]?.add(bean)
            }else{
                map[bean.regiontype] = arrayListOf(bean)
            }
        }

       val regionLists=ArrayList<ArrayList<RyRegionData>>()
        for (key in map.keys){
            map[key]?.let { regionLists.add(it) }
        }
        return regionLists
    }

    fun getMainData() :LiveData<RyMainData>{
        return ryMainData
    }

    fun getAlarmData():LiveData<RyAlarmData> {
        return ryAlarmData
    }

    fun getPersonData(type: String):LiveData<List<RyPersonData>>{
        ryPersonData=MutableLiveData<List<RyPersonData>>().also { loadRyPersonData(type) }
        return ryPersonData
    }

    fun getReportData():LiveData<RyReportData>{
        return ryReportData
    }
}